/**
 *
 */
package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.domain.Sort;
import com.ybkj.daijia.page.domain.Sort.Direction;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.AdminRoleMapper;
import com.ybkj.daijia.server.mapper.RoleAuthorityMapper;
import com.ybkj.daijia.server.mapper.RoleMapper;
import com.ybkj.daijia.server.model.Role;
import com.ybkj.daijia.server.model.RoleAuthority;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.RoleService;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author Shine
 *
 */
@Service
public class DefaultRoleService implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private RoleAuthorityMapper roleAuthorityMapper;

    @Autowired
    private CompanyService companyService;

    /* (non-Javadoc)
     * @see com.ybkj.shop.service.RoleService#queryAll()
     */
    public Page<Role> queryAll(final Long companyId, final String name) {

        Sort sort = new Sort(Direction.DESC, "id");

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit(), sort);

        List<Role> roles = roleMapper
            .selectByNameLike(companyId, name, Pager.getOffset(), Pager.getLimit());
//		for(Role role : roles){
//			Company company = companyService.findOne(role.getCompanyId());
//			if(null != company){
//				role.setCompanyName(company.getCompanyName());
//			}
//		}
        long total = roleMapper.countByNameLike(companyId, name);

        return new PageImpl<Role>(roles, pageable, total);
    }

    @Transactional(rollbackFor = Exception.class)
    public Role insertRole(Role role) {

        //保存角色信息
        roleMapper.insertRecord(role);

        //保存权限信息
        List<String> perms = role.getAuthorities();
        if (!CollectionUtils.isEmpty(perms)) {
            List<RoleAuthority> roleAuthorities = new LinkedList<RoleAuthority>();
            for (String perm : perms) {
                RoleAuthority roleAuthority = new RoleAuthority(role.getId(), perm);
                roleAuthorities.add(roleAuthority);
            }

            roleAuthorityMapper.batchInsertRecord(roleAuthorities);
        }

        return role;
    }

    public Role findOne(Long id) {

        return roleMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(Role role) {

        //删除角色与管理员管理信息
        adminRoleMapper.deleteByRoleId(role.getId());
        //删除角色与权限关联信息
        roleAuthorityMapper.deleteByRoleId(role.getId());
        //删除角色信息
        roleMapper.deleteByPrimaryKey(role.getId());
    }

    public Role findByName(String name) {

        return roleMapper.selectByName(name);
    }

    public List<Role> findAll() {

        return roleMapper.selectAll();
    }

    public List<Role> findByIdIn(Long[] roleIds) {

        return roleMapper.selectByIdIn(roleIds);
    }

    @Override
    public List<Role> findByAdmin(final Long adminId) {

        return roleMapper.selectByAdminId(adminId);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long updateByPrimaryKey(Role role) {

        //删除角色与权限关联信息
        roleAuthorityMapper.deleteByRoleId(role.getId());
        //保存权限信息
        List<String> perms = role.getAuthorities();
        if (!CollectionUtils.isEmpty(perms)) {
            List<RoleAuthority> roleAuthorities = new LinkedList<RoleAuthority>();
            for (String perm : perms) {
                RoleAuthority roleAuthority = new RoleAuthority(role.getId(), perm);
                roleAuthorities.add(roleAuthority);
            }

            roleAuthorityMapper.batchInsertRecord(roleAuthorities);
        }
        //更新角色本身的信息
        return roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public Role findOneWithPerms(Long id) {

        Role role = roleMapper.selectByPrimaryKey(id);
        if (null != role) {
            List<RoleAuthority> roleAuthorities = roleAuthorityMapper.selectByRoleId(id);
            if (!CollectionUtils.isEmpty(roleAuthorities)) {
                for (RoleAuthority roleAuthority : roleAuthorities) {
                    role.getAuthorities().add(roleAuthority.getAuthority());
                }
            }
        }

        return role;
    }

    @Override
    public List<Role> listByCompanyId(Long companyId) {
        return roleMapper.listByCompanyId(companyId);
    }
}
